package com.jnu.myAccount.record;

import android.content.Context;
import android.graphics.Color;

import com.jnu.myAccount.MyTextInputWatcher;
import com.jnu.myAccount.activity.MainActivity;

import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class AccountFundInfo{
    //账户名称
    private String name;
    //账户余额
    private float balance;
    private String date;
    //创建日期
    private String createDate;
    //账户类型
    private String type;
    //账户备注
    private String addition;
    //账户主题色
    private int color;
    //账户记录
    private Map<String, FundAdapter> fundInfoMap;

    public AccountFundInfo(String name) {
        this.name = name;
        this.balance = 0;
        this.createDate = MyTextInputWatcher.getStandardDateString(new Date(System.currentTimeMillis()));
        this.type = "现金";
        this.addition = "";
        this.color = Color.RED;
        this.fundInfoMap = new HashMap<>();
    }

    public Map<String, FundAdapter> getFundInfoMap() {
        return fundInfoMap;
    }

    //添加流水记录
    public void addFundInfo(FundAdapter info)
    {
        //判断账户是否相同
        if(info.getAccount().equals(name))
        {
            String date = info.getDate();
            //若map中不存在该记录则添加其引用
            if(!fundInfoMap.containsKey(date))
            {
                fundInfoMap.put(date, info);
            }
        }
    }

    public void setBalance(float balance) {
        this.balance = balance;
        setDate();
        saveData();
    }

    private void setDate()
    {
        date = MyTextInputWatcher.getStandardDateString(new Date(System.currentTimeMillis()));
    }

    //加载账户余额数据
    public void loadData()
    {
        try {
            ArrayList<String> result = new ArrayList<>();
            File file = MainActivity.ContextInstance().getFileStreamPath("").getAbsoluteFile();
            File[] files = file.listFiles();
            for (int i = 0; i < Objects.requireNonNull(files).length; ++i) {
                if (!files[i].isDirectory()) {
                    String fileName = files[i].getName();
                    result.add(fileName);
                }
            }
            if (result.contains(name+"Balance")){
                ObjectInputStream in = new ObjectInputStream(
                        MainActivity.ContextInstance().openFileInput(name+"Balance")
                );
                balance = in.readFloat();
                date = in.readUTF();
                if(date==null||date.length()==0)
                {
                    setDate();
                }
                in.close();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //保存账户余额数据
    public void saveData()
    {
        try{
            ObjectOutputStream out = new ObjectOutputStream(
                    MainActivity.ContextInstance().openFileOutput(name+"Balance", Context.MODE_PRIVATE)
            );
            out.writeFloat(balance);
            out.writeUTF(date);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String getName() {
        return name;
    }

    public float getBalance() {
        return balance;
    }

    public String getCreateDate() {
        return createDate;
    }

    public String getType() {
        return type;
    }

    public String getAddition() {
        return addition;
    }

    public int getColor() {
        return color;
    }
}

